package com.fingard.dsp.bank.directbank.bon01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BON01Balance extends BON01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            BON01Header bankHeader = new BON01Header();
            bankHeader.trCode = "200108";
            bankHeader.reqSeqNo = p_req7506.reqHeader.transSeqID;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("body");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("acno", p_req7506.readedDetail.actNum);
                xmlWriter.writeElementString("cur_code", convertCurToBank(p_req7506.readedDetail.actCur));
            }
            xmlWriter.writeEndElement();//body
            xmlWriter.writeEndElement();//ap
            xmlWriter.writeEndDocument();

            String[] retStrArr = sendToBank(xmlWriter.toXmlString());
            if (retStrArr[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);

                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/ap/head/ret_code");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/head/ret_info");
                    String tmpSuccFlag = tmpBkReader.getFirstText("/ap/head/succ_flag");
                    if ("0000".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/ap/body")) {
                            RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                            tmpRetDetail.respCode = tmpSuccFlag;
                            tmpRetDetail.respInfo = tmpRetInfo;
                            tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("acno");
                            tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("cur_code"));
                            tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("balance");
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("use_balance");
                            tmpRetDetail.frozenBal = tmpBkReader.readedXElement.getSingleText("freeze_bal");
                            retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                        }
                    } else {
                        RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpRetInfo;
                        retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(retStrArr[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
